﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace Example6_Particles.View
{
    class ExampleParticleSystem
    {
        private const int NUM_PARTICLES = 1000;
        ExampleParticle[] m_particles = new ExampleParticle[NUM_PARTICLES];

        public ExampleParticleSystem()
        {
            Random rand = new Random(10);

            Vector2 pos = new Vector2(0, 0);

            float speed = 10.0f;
            for (int i = 0; i < NUM_PARTICLES; i++)
            {
                Vector2 velocity = new Vector2((2.0f * (float)rand.NextDouble() - 1.0f),
                                               (2.0f * (float)rand.NextDouble() - 1.0f));

                velocity.Normalize(); //sätt längden

                velocity *= speed * (float)rand.NextDouble() ;

                m_particles[i] = new ExampleParticle(pos, velocity);
            }
        }


        internal void Draw(Microsoft.Xna.Framework.Graphics.SpriteBatch a_spriteBatch, Microsoft.Xna.Framework.Graphics.Texture2D a_sparkTexture, float a_elapsedTime)
        {

            for (int i = 0; i < NUM_PARTICLES; i++)
            {
                m_particles[i].Update(a_elapsedTime / 10.0f);

                Vector2 modelPosition = m_particles[i].GetPosition();


                Vector2 viewPos = modelPosition * 100.0f + new Vector2(400, 300);
                int particleViewSize = 50;

                Rectangle destinationRectangle = new Rectangle((int)viewPos.X - particleViewSize / 2, (int)viewPos.Y - particleViewSize / 2, particleViewSize, particleViewSize); 
                a_spriteBatch.Draw(a_sparkTexture, destinationRectangle, Color.White);
            }
        }
    }
}
